home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DIRS.SWG / 0035_ExistDir.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  3KB  |  79 lines

  1. (*
  2.  
  3.   Here are three functions that I wrote to detect directories. The first 
  4.   one uses findfirst, the second uses chdir and the third uses getfattr.
  5.   According to my benchmarkings the third one is the fastest (and the one 
  6.   I preffer) . All of them need the DOS unit and will do the job as 
  7.   requested, however, they are not exactly equivalent: The first function
  8.   will return false for d:= '<disk>:\', '\' or '..\'. They all return true 
  9.   if the drive has been SUBSTituted.
  10.  
  11.   Here are the results with some extreme strings (T = true, F = false)...
  12.  
  13.          Function --->      1      2      3 
  14.          -----------------------------------
  15.           d:=  ''           F      F      F
  16.           d:=  '.'          T      T      T
  17.           d:=  '..'         F      T      T    (*)
  18.           d:=  '.\'         F      F (@)  T
  19.           d:=  '..\'        F      T      T    (*)
  20.           d:=  '\'          F      T      T
  21.           d:=  '/'          F      T (#)  F  
  22.           d:=  'c:\'        F      T      T
  23.           d:=  'c:\.'       F      T      T
  24.  
  25.       (*)  while logged in a non-root directory.
  26.       (@)  chdir('.\') is not recognized as a valid change!
  27.       (#)  chdir('/') switches to the root!
  28.  
  29.   In all other situations the three functions return the same result.
  30.     
  31.   ---------------[cut]-----------------------------------------------
  32.   
  33.   function direxist1(d:pathstr): boolean;
  34.   var
  35.     dirinfo: searchrec;
  36.     len : byte;
  37.   begin
  38.     len:= length(d);
  39.     if (d[len] = '\') and          {if d has a trailing slash and is...  }
  40.     (len > 3) then                 {other than "<disk>:\", "..\"...      }
  41.       dec(d[0]);                   {remove the trailing slash.           }
  42.     findfirst(d,directory,dirinfo);{call findfirst.                      }
  43.     direxist1:= doserror = 0;      {report boolean result                }
  44.   end;
  45.  
  46.   function direxist2(d:pathstr) : boolean;
  47.   var
  48.     curdir: pathstr;
  49.     exist : boolean;
  50.     len   : byte;
  51.   begin
  52.     len:= length(d);
  53.     if (d[len] = '\') and          {if d has a trailing slash and is...  }
  54.     (len > 3) then                 {other than "<disk>:\" or "..\"...    }
  55.       dec(d[0]);                   {remove the trailing slash.           }
  56.     getdir(0,curdir);              {get current dir                      }
  57.     {$I-} chdir(d); {$I+}          {attempt changing directory           }
  58.     exist := IOResult = 0;         {test IOResult                        }
  59.     if exist then chdir(curdir);   {if exist then go back to current dir }
  60.     direxist2:= (d <> '') and exist;
  61.   end;
  62.  
  63.   function direxist3(d: pathstr): boolean;
  64.   var
  65.     f   : file;
  66.     attr: word;
  67.     len : byte;
  68.   begin
  69.     len:= length(d);
  70.     if (d[len] = '\') then         {if d has a trailing slash...         }
  71.       dec(d[0]);                   {remove the trailing slash.           }
  72.     d:= d + '\.';                  {add '\.' to d                        }
  73.     assign(f,d);                   {assign d to f                        }
  74.     getfattr(f,attr);              {get the attribute word               }
  75.     direxist3 := ((attr and directory)=directory);
  76.                                    {return true if attr is directory     }
  77.   end;
  78.  
  79.